package com.tj.jinzao.activity;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nim.uikit.business.recent.RecentContactsCallback;
import com.netease.nim.uikit.business.recent.RecentContactsFragment;
import com.netease.nim.uikit.business.session.constant.Extras;
import com.netease.nim.uikit.impl.NimUIKitImpl;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.tandong.bottomview.view.BottomView;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 最近聊天页面
 * Created by wjy on 2019/1/5.
 */
public class RecentActivity extends BaseActivity {

    @Override
    public int getLayoutResource() {
        return R.layout.base_activity_fragment_layout;
    }

    @Override
    protected Fragment createFragment() {
        return getRecentContactsFragment();
    }


    @Override
    protected int getRightButtonResId() {
        return R.drawable.add;
    }
    private BottomView bottomView;
    private void showPopWindow(){
        bottomView=new BottomView(RecentActivity.this, com.netease.nim.uikit.R.style.BottomViewTheme_Defalut, com.netease.nim.uikit.R.layout.item_select);
        bottomView.setAnimation(com.netease.nim.uikit.R.style.myStyle);
        bottomView.dismissBottomView();
        bottomView.showBottomView(false);
        TextView txt_1= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_1);
        TextView txt_2= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_2);
        txt_1.setText("新建群组");
        txt_2.setText("我的好友");
        TextView txt_exit= bottomView.getView().findViewById(com.netease.nim.uikit.R.id.txt_exit);
        txt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent to=new Intent();
                to.setClass(RecentActivity.this,CreateGroupActivity.class);
                startActivity(to);
                bottomView.dismissBottomView();

            }
        });
        txt_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent to=new Intent();
                to.setClass(RecentActivity.this,FriendActivity.class);
                startActivity(to);
                bottomView.dismissBottomView();
            }
        });
        txt_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomView.dismissBottomView();
            }
        });

    }

    @Override
    protected void clickRightButton() {
        showPopWindow();
    }

    // 将最近联系人列表fragment动态集成进来。 开发者也可以使用在xml中配置的方式静态集成。
    private Fragment getRecentContactsFragment() {
        RecentContactsFragment fragment = new RecentContactsFragment();
        fragment.setContainerId(R.id.fragment_container);

//        final UI activity = (UI) getActivity();

        // 如果是activity从堆栈恢复，FM中已经存在恢复而来的fragment，此时会使用恢复来的，而new出来这个会被丢弃掉
        fragment.setCallback(new RecentContactsCallback() {
            @Override
            public void onRecentContactsLoaded() {
                // 最近联系人列表加载完毕
            }

            @Override
            public void onUnreadCountChange(int unreadCount) {
//                未读数量
//                ReminderManager.getInstance().updateSessionUnreadNum(unreadCount);
            }

            @Override
            public void onItemClick(RecentContact recent) {
                // 回调函数，以供打开会话窗口时传入定制化参数，或者做其他动作
                switch (recent.getSessionType()) {
                    case P2P:
                        Intent intent = new Intent();
                        intent.putExtra(Extras.EXTRA_ACCOUNT, recent.getContactId());
                        intent.putExtra(Extras.EXTRA_CUSTOMIZATION, NimUIKitImpl.commonP2PSessionCustomization);
                        intent.setClass(RecentActivity.this, ChatActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);

//                        NimUIKit.startP2PSession(RecentActivity.this, recent.getContactId(), null);
                        break;
                    case Team:
                        NimUIKit.startTeamSession(RecentActivity.this, recent.getContactId(), null);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public String getDigestOfAttachment(RecentContact recentContact, MsgAttachment attachment) {
                // 设置自定义消息的摘要消息，展示在最近联系人列表的消息缩略栏上
                // 当然，你也可以自定义一些内建消息的缩略语，例如图片，语音，音视频会话等，自定义的缩略语会被优先使用。
                return null;
            }

            @Override
            public String getDigestOfTipMsg(RecentContact recent) {
                String msgId = recent.getRecentMessageId();
                List<String> uuids = new ArrayList<>(1);
                uuids.add(msgId);
                List<IMMessage> msgs = NIMClient.getService(MsgService.class).queryMessageListByUuidBlock(uuids);
                if (msgs != null && !msgs.isEmpty()) {
                    IMMessage msg = msgs.get(0);
                    Map<String, Object> content = msg.getRemoteExtension();
                    if (content != null && !content.isEmpty()) {
                        return (String) content.get("content");
                    }
                }

                return null;
            }
        });
        return fragment;
    }
    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

}
